package com.dgut.smart_house.controller;

import com.dgut.smart_house.domain.Device;
import com.dgut.smart_house.dto.DeviceNodeDto;
import com.dgut.smart_house.dto.RealDataDto;
import com.dgut.smart_house.dto.UserDeviceDto;
import com.dgut.smart_house.enums.ResultCode;
import com.dgut.smart_house.exception.MyException;
import com.dgut.smart_house.service.DeviceListService;
import com.dgut.smart_house.service.UserService;
import com.dgut.smart_house.util.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/data/api")
public class ApiController {

    @Autowired
    private DeviceListService deviceListService;

    @Autowired
    private UserService userService;

    /**
     * 分页查询所有数据
     *
     * @param devId
     * @param pageNo
     * @param pageSize
     * @return
     * @throws IOException
     */
    @GetMapping("/device/{devId}")
    public RestResult data2Deviceapi(@PathVariable("devId") Integer devId,
                                     @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                     @RequestParam(value = "pageSize", defaultValue = "5") int pageSize
    ) throws MyException {
        return new RestResult(ResultCode.SUCCESS, deviceListService.findDeviceDataByDevId(devId, pageNo, pageSize));
    }

    /**
     * 实时数据
     *
     * @return
     */
    @RequestMapping("/realData")
    public RestResult realData(HttpSession session) {
        List<RealDataDto> list = deviceListService.findRealDatas((int) session.getAttribute("IdFromUserTable"));
        return new RestResult(ResultCode.SUCCESS, list);
    }

    /**
     * 查询登录用户拥有的所有设备
     *
     * @return
     */
    @GetMapping("/deviceSet")
    public RestResult deviceSet(HttpSession session) {

        List<UserDeviceDto> deviceDtos = deviceListService.findAllDevices((int) session.getAttribute("IdFromUserTable"));
        return new RestResult(ResultCode.SUCCESS, deviceDtos);
    }

    /**
     * 删除指定设备
     *
     * @param devId
     * @return
     */
    @DeleteMapping("/deviceSet/{devId}")
    public RestResult deleteDevice(@PathVariable("devId") Integer devId, HttpSession session) {

        if ((int) session.getAttribute("IdFromUserTable") != deviceListService.findUserByDevId(devId)) {
            return new RestResult(ResultCode.NO_AUTH_FAIL, devId);
        } else {
            DeviceNodeDto device = deviceListService.findDeviceByDevId(devId);
            deviceListService.deleteDevice(devId);
            deviceListService.deleteDatasByDevId(devId);
            log.info("用户-{}-成功删除位于{}-的设备-{}", session.getAttribute("userId"), device.getDeviceDesc(), device.getNodeName());
            return new RestResult(ResultCode.SUCCESS.getCode(), "删除成功", null);
        }
    }

    /**
     * 修改设备信息
     *
     * @param devId
     * @param desc
     * @return
     */
    @PutMapping("/deviceSet/{devId}")
    public RestResult modifyDevice(@PathVariable("devId") Integer devId,
                                   @RequestParam("desc") String desc,
                                   HttpSession session) {

        if ((int) session.getAttribute("IdFromUserTable") != deviceListService.findUserByDevId(devId)) {
            return new RestResult(ResultCode.NO_AUTH_FAIL, null);
        } else {
            deviceListService.updateDevice(devId, desc);
            log.info("用户-{}-修改设备id-{}-位置为-{}", session.getAttribute("userId"), devId, desc);
            return new RestResult(ResultCode.SUCCESS, null);
        }
    }

    /**
     * 添加设备
     *
     * @param device
     * @return
     */
    @PostMapping("/deviceSet")
    public RestResult createDevice(Device device, HttpSession session) {
        device.setFromUser((int) session.getAttribute("IdFromUserTable"));
        deviceListService.createDevice(device);
        log.info("用户-{}-成功添加位于-{}-的设备node_id-{}", session.getAttribute("userId"), device.getDevDesc(), device.getNodeId());
        return new RestResult(ResultCode.SUCCESS.getCode(), "设备添加成功", null);
    }

    /**
     * 查询数据库所有设备种类
     *
     * @return
     */
    @RequestMapping("/deviceSet/addDevice")
    public RestResult findAllDevice() {
        return new RestResult(ResultCode.SUCCESS, deviceListService.findAllNodes());
    }


}
